package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.TaskDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoji on 2014/11/3.
 */
@Controller
@RequestMapping("taskdetail")
public class TaskDetailController {
    @Autowired
    private TaskDetailService taskDetailService;

    @RequestMapping("index")
    public String index(Model model,Integer taskId,HttpServletRequest request){

        Map<String, Object> detail = taskDetailService.getTaskDetailByTaskId(taskId);
        //任务信息
        model.addAttribute("detail",detail);
        //竞标信息
        //model.addAttribute("binfos",taskDetailService.getBusBidinfo(taskId));
        //任务进程信息
        model.addAttribute("processes",taskDetailService.getProcesses());
        //是否竞标
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        if(userInfo !=null){
            model.addAttribute("isbid",taskDetailService.getUserBidinfoByTaskId(taskId,userInfo.getUserId()));
        }else{
            model.addAttribute("isbid", -1);
        }

        //如果是发包方是否显示中标按钮
        if(userInfo !=null) {//用户登录
            if (Integer.parseInt(detail.get("userId").toString()) == userInfo.getUserId()) {//登录用户为改任务发包方
                model.addAttribute("isMy", 1);
                Map<String, Object> map = taskDetailService.getWinTotalByItemId(taskId);
                if (map != null) {
                    if (Integer.parseInt(map.get("total").toString()) == 0) {//未中标
                        model.addAttribute("isBidButton", 1);
                    }
                }
            }
        }

        if(detail.get("isPiece")!=null && Integer.parseInt(detail.get("isPiece").toString()) == 101001){//计件任务
            return "/taskdetail/indexjq";
        }else{//普通任务
            return "/taskdetail/index";
        }

    }
    @RequestMapping(value = "getBidInfo",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getBidInfo(Integer page,Integer rows,
                                          Integer taskId){
        Map<String, Object> map = new HashMap<String,Object>();
        PageHelper.startPage(page, rows);
        List<Map<String, Object>> list = taskDetailService.getBusBidinfo(taskId);
        PageInfo pageInfo = new PageInfo(list);
        map.put("pageInfo",pageInfo);
        return  map;
    }

    /**
     * 设置中标
     * @param request
     * @param itemId 任务编号
     * @param id 投标编号
     * @return
     */
    @RequestMapping(value = "winItem",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> winItem(HttpServletRequest request,int itemId,int id){
        Map<String, Object> map = new HashMap<String,Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        try {
            taskDetailService.updateWinItem(id,itemId,userInfo.getUserId());
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return  map;
    }

    /**
     * 设置计件中标
     * @param request
     * @param itemId 任务编号
     * @param ids 计件投标中标编号
     * @return
     */
    @RequestMapping(value = "winTotalNumItem",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> winTotalNumItem(HttpServletRequest request,int itemId,String ids){
        Map<String, Object> map = new HashMap<String,Object>();
        UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");
        try {
            taskDetailService.updateTotalNumWinItem(ids,itemId,userInfo.getUserId());
        } catch (Exception e) {
            map.put("errormsg",e.getMessage());
        }
        return  map;
    }

}
